package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.DataFrameBo;
import com.ruoyi.system.domain.vo.DataFrameVo;
import com.ruoyi.system.domain.DataFrame;
import com.ruoyi.system.mapper.DataFrameMapper;
import com.ruoyi.system.service.IDataFrameService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 数据帧Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-30
 */
@RequiredArgsConstructor
@Service
public class DataFrameServiceImpl implements IDataFrameService {

    private final DataFrameMapper baseMapper;

    /**
     * 查询数据帧
     */
    @Override
    public DataFrameVo queryById(Long dataFrameId){
        return baseMapper.selectVoById(dataFrameId);
    }

    /**
     * 查询数据帧列表
     */
    @Override
    public TableDataInfo<DataFrameVo> queryPageList(DataFrameBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DataFrame> lqw = buildQueryWrapper(bo);
        Page<DataFrameVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询数据帧列表
     */
    @Override
    public List<DataFrameVo> queryList(DataFrameBo bo) {
        LambdaQueryWrapper<DataFrame> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DataFrame> buildQueryWrapper(DataFrameBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DataFrame> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLength() != null, DataFrame::getLength, bo.getLength());
        lqw.eq(bo.getPriority() != null, DataFrame::getPriority, bo.getPriority());
        lqw.eq(StringUtils.isNotBlank(bo.getCrcCode()), DataFrame::getCrcCode, bo.getCrcCode());
        lqw.eq(bo.getInport() != null, DataFrame::getInport, bo.getInport());
        lqw.eq(bo.getSendTime() != null, DataFrame::getSendTime, bo.getSendTime());
        lqw.eq(bo.getSendState() != null, DataFrame::getSendState, bo.getSendState());
        lqw.eq(bo.getDocumentId() != null, DataFrame::getDocumentId, bo.getDocumentId());
        lqw.eq(bo.getSequenceNumber() != null, DataFrame::getSequenceNumber, bo.getSequenceNumber());
        return lqw;
    }

    /**
     * 新增数据帧
     */
    @Override
    public Boolean insertByBo(DataFrameBo bo) {
        DataFrame add = BeanUtil.toBean(bo, DataFrame.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDataFrameId(add.getDataFrameId());
        }
        return flag;
    }

    /**
     * 修改数据帧
     */
    @Override
    public Boolean updateByBo(DataFrameBo bo) {
        DataFrame update = BeanUtil.toBean(bo, DataFrame.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DataFrame entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除数据帧
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
